package com.zxjbyte.yiyi.framework.quartz.alarm;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.zxjbyte.yiyi.framework.common.constant.Constant;
import com.zxjbyte.yiyi.framework.common.domain.mail.MailMetadata;
import com.zxjbyte.yiyi.framework.common.util.EmailUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhangxingjia
 * @Date 2024/1/23 15:32
 * @Version: 1.0
 */
@Slf4j
public class EmailAlarmSender implements IJobAlarm {

    public static EmailAlarmSender instance = null;

    public static EmailAlarmSender getInstance() {
        if(instance == null){
            instance = new EmailAlarmSender();
        }
        return instance;
    }

    @Override
    public boolean doAlarm(JobAlarmContext context) {
        boolean alarmResult = true;
        Map<String, String> map = new HashMap<>(8);
        map.put("ITEM.NAME", SpringUtil.getApplicationName());
        map.put("HOST.NAME", NetUtil.getLocalHostName() + " [主机IP: "+ NetUtil.getLocalhostStr() +"]");
        map.put("ITEM.TYPE", context.getTypeEnum().getDesc());
        map.put("ITEM.TASK_KEY", context.getJobKey());
        map.put("ITEM.AUTHOR", context.getAuthor());
        map.put("TRIGGER.EXPRESSION", context.getExpression());
        map.put("EVENT.DATE", DateUtil.format(context.getAlarmTime(), Constant.DATETIME_MS_FORMAT));
        map.put("EXCEPTION.INFO", context.getErrMsg());
        try{
            EmailUtil.sendHtmlEmail(
                    MailMetadata.of(Constant.MAIL_FORM, context.getAlarmEmail(), null, null, "任务调度中心监控报警", StrUtil.format(template(), map))
            );
        } catch (Exception ex) {
            log.error("job alarm mail >>>> send error, id: {}, error: {}", context.getJobId(), ex);
            alarmResult = false;
        }
        return alarmResult;
    }

    public String template(){
        return "<html>\n" +
                "<body>\n" +
                " <style>\n" +
                "  table{font-size:14px;font-family:微软雅黑}\n" +
                " </style>\n" +
                " <div align=\"center\">\n" +
                " <table border=\"1\" style=\"border:5px solid #F2F2F2;\" cellspacing=\"2\" cellpadding=\"4\" width=\"900\" style=\"table-layout:fixed\">\n" +
                "  <tr bgcolor=\"#D1D1D1\">\n" +
                "    <th align=\"center\" style=\"font-size:23px;\">告警信息</marquee></th>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "   <td align=\"left\" style=\"WORD-WRAP:break-word\"><span style=\"color:#FF0000; display:inline-block; width:110px\">告警项目 :</span>{ITEM.NAME}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "   <td align=\"left\" style=\"WORD-WRAP:break-word\"><span style=\"color:#FF0000; display:inline-block; width:110px\">告警主机 :</span>{HOST.NAME}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "   <td align=\"left\" style=\"WORD-WRAP:break-word\"><span style=\"color:#FF0000; display:inline-block; width:110px\">告警类别 :</span>{ITEM.TYPE}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "   <td align=\"left\" style=\"WORD-WRAP:break-word\"><span style=\"color:#FF0000; display:inline-block; width:110px\">告警任务KEY :</span>{ITEM.TASK_KEY}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "   <td align=\"left\" style=\"WORD-WRAP:break-word\"><span style=\"color:#FF0000; display:inline-block; width:110px\">任务负责人 :</span>{ITEM.AUTHOR}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "   <td align=\"left\" style=\"WORD-WRAP:break-word\"><span style=\"color:#FF0000; display:inline-block; width:110px\">告警触发规则 :</span>{TRIGGER.EXPRESSION}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "   <td align=\"left\" style=\"WORD-WRAP:break-word\"><span style=\"color:#FF0000; display:inline-block; width:110px\">告警时间 :</span>{EVENT.DATE}</td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "   <td align=\"left\" style=\"WORD-WRAP:break-word\"><span style=\"color:#FF0000; display:inline-block; width:110px\">告警堆栈信息 :</span>{EXCEPTION.INFO}</td>\n" +
                "  </tr>\n" +
                " </table>\n" +
                " <p>此邮件为系统自动发送，请勿回复!</p>\n" +
                "</body>\n" +
                "</html>";
    }
}
